const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'
import {
    reqPlayList,
} from '../../../../request/api'
Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        dataList: [], //列表 
        page: 1,
        isNextPage: true, //是否下一页
        triggered: false, //是否下拉刷新列表
        currentVideoContext: null, // 当前播放视频的上下文
    },

    onLoad(e) {
        // console.log(e);
        let info = JSON.parse(e.info)
        this.setData({
            info
        })
    },
    onShow() {
        this.getInitDataList()
    },
    // bindplay="handleVideoPlay" id="{{'video' + index}}"
    // 视频播放事件处理函数 播放第二个视频暂停第一个视频
    handleVideoPlay(e) {
        // console.log(e.target);
        // const videoId = e.target.id; // 获取触发播放事件的video的id
        // const videoContext = wx.createVideoContext(videoId); // 创建或获取VideoContext实例

        // // 如果当前有播放的视频，则暂停
        // if (this.data.currentVideoContext) {
        //     this.data.currentVideoContext.pause();//stop();
        // }

        // // 更新当前播放的视频上下文
        // this.setData({
        //     currentVideoContext: videoContext
        // });

        // // 播放当前视频
        // videoContext.play();
    },
    async getDataList() {
        const res = await reqPlayList(this.data.page, this.data.info.imei)
        if (res.code !== 20000) return showMsg(res.message, "error")
        let {
            rows
        } = res.data
        if (this.data.page === 1) {
            this.setData({
                dataList: rows,
                isNextPage: true
            })
        } else if (this.data.page > 1 && rows.length) {
            this.setData({
                dataList: this.data.dataList.concat(rows),
                isNextPage: true
            })
        } else if (this.data.page > 1 && !rows.length) {
            showMsg('已经到底啦~')
            this.setData({
                isNextPage: false
            })
        }
    },
    // 滚动到底部触发
    lower() {
        if (!this.data.isNextPage) return
        this.setData({
            page: this.data.page + 1,
        })
        this.getDataList()
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            page: 1,
            isNextPage: true,
            triggered: true,
        })
        await this.getDataList()
        this.setData({
            triggered: false,
        })
    },

    // 操作列表后回到最初状态
    getInitDataList() {
        this.setData({
            page: 1,
            isNextPage: true,
            dataList: []
        })
        this.getDataList()
    },
})